/**
 * Created by tianzx on 16/8/17.
 */
// This file merely configures the store for hot reloading.
// This boilerplate file is likely to be the same for each project that uses Redux.
// With Redux, the actual stores are in /reducers.

import { createStore, applyMiddleware, compose } from 'redux';

import rootReducer from '../reducers';
import createLogger from 'redux-logger';
import thunk from 'redux-thunk';
import { browserHistory } from 'react-router';
import { routerMiddleware } from 'react-router-redux';
import promiseMiddleware from '../middlewares/promiseMiddleware';
const logger = createLogger();
const router = routerMiddleware(browserHistory);

const enhancer = compose(
    applyMiddleware(thunk, promiseMiddleware({promiseTypeSuffixes:['PENDING','SUCCESS','ERROR']}), logger, router),
    window.devToolsExtension ? window.devToolsExtension() : f => f
);

export default function configureStore(initialState) {
    const store = createStore(rootReducer,
        initialState, enhancer
    );

    if (module.hot) {
        // Enable Webpack hot module replacement for reducers
        module.hot.accept('../reducers', () => {
            const nextReducer = require('../reducers').default; // eslint-disable-line global-require
            store.replaceReducer(nextReducer);
        });
    }

    return store;
}
